$(function(){
	$("#icon-plus").show();
	$("#icon-minus").hide();
});
function showRoadTr(){
	$("#roadTr").show();
	$("#icon-minus").show();
	$("#icon-plus").hide();		
}
function hideRoadTr(){
	$("#roadTr").hide();
	$("#icon-plus").show();
	$("#icon-minus").hide();
}

var mydialog;

function c_search(){
	mydialog=art.dialog({title:"處理中",lock:true});
	
	var datas=new Array();
	var datas_id=new Array();
	var created_at1=$("#de_created_at_1").val();
	var created_at2=$("#de_created_at_2").val();
	if(created_at1!=''&&created_at2!=''){
		$("#de_created_at").val(created_at1+','+created_at2);
	}else{
		$("#de_created_at").val('');
	}
	var data=$(".post").get();
	var i=0;
	$.each(data,function(key,value){
		if(value["type"]==='radio'){
			if(value["checked"]){
				datas[i]=$(this).val();
				datas_id[i]=value["id"];
			}
		}else if(value["type"]==='checkbox'){
			if(value["checked"]){
				datas[i]=$(this).val()+",";
				datas_id[i]=value["id"];
			}
		}else{
			if($(this).val()!=''&&$(this).val()!=undefined){
				datas[i]=$(this).val();
				datas_id[i]=value["id"];
			}
		}
		i++;
	});
	
	$.ajax({
		type:"post",
		url:"demand_search",
		dataType:"json",
		data:{operator:"4",s_c:datas,ids:datas_id},
		success:c_search_result
	});
}

function c_search_result(data){
	if(data.status===1){
		mydialog.content("非法訪問，不能查詢").title("提示");
	}else if(data.status===0){
		mydialog.content("沒有權限，不能查詢").title("提示");
	}else if(data.status===3){
		mydialog.close();
		$("#result").html("<center>暫無查詢結果</center>");
		//$("#result").html(data.data);
	}else if(data.status===2){
		mydialog.close();
		var table="<table id='results'></table>";
		$("#result").html(table);
		$("#result #results").append("<thead><tr><th>操作</th><th>配對</th><th>客戶姓名</th><th>需求等級</th><th>建物面積(坪)</th><th>室內面積(坪)</th><th>土地面積(坪)</th><th>路段</th><th>區域</th><th>商圈</th><th>總預算(萬)</th><th>經紀人</th><th>建檔日期</th><th>物件用途</th><th>需求別</th><th>營業項目</th><th></th></thead>");
		$("#result #results").append("<tbody>");
		$.each(data.data,function(key,value){
			var demand_type="";
			if(value['de_de']==='1'){
				demand_type="租案";
			}else if(value['de_de']==='2'){
				demand_type="售案";
			}else if(value['de_de']==='3'){
				demand_type="代銷";
			}
			$("#result #results").append("<tr><td><input type='button' value='操作' onclick=S_operator("+value["id"]+",'"+value["cu_name"]+"'); /></td><td><input type='button' value='配對' onclick=single_pair("+value["id"]+"); /></td><td>"+value["cu_name"]+"</td><td>"+value["ba_impor"]+"</td><td>"+value["de_buil1"]+"~"+value["de_buil2"]+"</td><td>"+value["de_ind1"]+"~"+value["de_ind2"]+"</td><td>"+value["de_land1"]+"~"+value["de_land2"]+"</td><td>"+value["de_sec1"]+"</td><td>"+value["ba_area1"]+"</td><td>"+value["ba_distr1"]+"</td><td>"+value["de_total1"]+"~"+value["de_total2"]+"</td><td>"+value["cu_broker1"]+"</td><td>"+value["de_created_at"]+"</td><td>"+value["ba_use"]+"</td><td>"+demand_type+"</td><td>"+value["de_item"]+"</td></tr>");
		});
		//$("#result #results").append("<tr><td colspan='16'>123456</td></tr>");
		$("#result #results").append("</tbody>");
		$("#result").append("<script type='text/javascript'>$('#results th').attr('width','80');</script>");
		$("#result").append("<link rel='stylesheet' type='text/css' href='../../public/css/flex.css' />");
		$("#result").append("<link rel='stylesheet' type='text/css' href='../../public/css/flexigrid.pack.css' />");
		$("#result").append("<script type='text/javascript' src='../../public/js/flexigrid.pack.js'></script>");
		$("#result").append("<script type='text/javascript' src='../../public/js/flextable.js'></script>");
		//$("#result").html(data.data);
	}
}

function S_operator(c_id,c_name){
	mydialog=art.dialog({
		title:"操作-"+c_name,
		content:"<a class='operator_alter' onclick='S_alter("+c_id+");'><i class='icon-edit'></i>修改</a>&nbsp;&nbsp;<a class='operator_del' onclick=S_delete('"+c_id+"','zb051h'); ><i class='icon-trash'></i>刪除</a>",
		lock:true
	});
}
function single_pair(id){
	$.ajax({
		url:'single_pair',
		type:'post',
		data:{id:id},
		dataType:'json',
		success:function(data){
			if(data.status == 0){
				alert("查無配對結果");
			}else if(data.status==1){
				var table = '<table width=\'800\' style="border-top:1px solid #000;border-left:1px solid #000;"><tr><th style="border-right:1px solid #000;border-bottom:1px solid #000;">物件編號/市調編號</th><th style="border-right:1px solid #000;border-bottom:1px solid #000;">物件狀態/市調狀態</th><th style="border-right:1px solid #000;border-bottom:1px solid #000;">案名</th><th style="border-right:1px solid #000;border-bottom:1px solid #000;">房屋售價/房屋租金</th><th style="border-right:1px solid #000;border-bottom:1px solid #000;">主建物面積</th><th style="border-right:1px solid #000;border-bottom:1px solid #000;">附屬建物面積</th><th style="border-right:1px solid #000;border-bottom:1px solid #000;">公設</th><th style="border-right:1px solid #000;border-bottom:1px solid #000;">車位</th><th style="border-right:1px solid #000;border-bottom:1px solid #000;">每月管理費</th>';
				$.each(data.data,function(key,value){
					table = table +'<tr><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["new_sn"]+'</td><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["new_type"]+'</td><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["ob_case"]+'</td><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["new_price"]+'</td><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["ob_mainbuil"]+'</td><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["ob_subsid"]+'</td><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["ob_post"]+'</td><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["ob_parkuse"]+'</td><td style="border-right:1px solid #000;border-bottom:1px solid #000;">'+value["ob_monfee"]+'</td></tr>';
				});
				table = table+"</tbody>";
				table = table+"</table>";
				mydialog=art.dialog({
					width:800,
					height:300,
					content:table					
				});
			}
		}
	});
}
function S_alter(id){
	//var w=window.innerWidth;
	//var h=window.innerHeight;
	//location.href="demand_alter?operator=27&&id="+id;
	window.open("demand_alter?operator=27&&id="+id,"_blank");
}

function S_delete(id,table){
	if(confirm("你确定删除吗？")){
		mydialog.content(null).title('处理中...');
		$.ajax({
			url:'del_action',
			type:'post',
			dataType:'json',
			data:{table:table,id:id,operator:'28'},
			success:function(data){
				if(data.status===3){
					mydialog.close();
					art.dialog({
						title:'提示',
						content:'刪除成功',
						lock:true,
						ok:function(){
							c_search();
					}
					});
				}else{
					mydialog.close();
					art.dialog({
						title:'提示',
						content:'刪除失敗',
						lock:true
					});
				}
			}
		});
	}
}

function getLineData(obj){
	var line=$(obj).find("option:selected").html();
	if(line!=''&&line!='&nbsp;'){
		$.ajax({
			url:'getLineData',
			type:'post',
			data:{line:line},
			dataType:'json',
			success:function(data){
				getLineData_status(data);
			}
		});
	}else{
		$('#station').html('');
	}
	
}

function getLineData_status(data){
	if(data.status===1){
		var options='';
		$.each(data.data,function(key,value){
			options+="<option value='"+value['id']+"'>"+value['ba_mrt']+"</option>";
		});
		$('#station').html(options);
		$('#station').append("<option value='' >&nbsp;</option>");
	}else if(data.status===2){
		alert('加載捷運站失敗');
	}
}